package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplus.mapper.AdminsMapper;
import com.example.mybatisplus.mapper.ViolationsMapper;
import com.example.mybatisplus.model.domain.Admins;
import com.example.mybatisplus.model.domain.Logs;
import com.example.mybatisplus.mapper.LogsMapper;
import com.example.mybatisplus.model.dto.LogDTO;
import com.example.mybatisplus.model.dto.SelectRequest;
import com.example.mybatisplus.service.LogsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2024-07-17
 */
@Service
public class LogsServiceImpl extends ServiceImpl<LogsMapper, Logs> implements LogsService {

    @Autowired
    private LogsMapper logsMapper;
    @Autowired
    private ViolationsMapper violationsMapper;
    @Autowired
    private AdminsMapper adminsMapper;
    @Override
    public void insertLog(Logs logs){
        logsMapper.insertAll(logs);
    }


    @Override
    public List<LogDTO> getAllLogs() {
        List<String> logIds = logsMapper.getAllLogsId();
        List<String> vioIds = logsMapper.getAllLogsVioId();
        List<LocalDateTime> operationTimes = logsMapper.getAllOperationTime();
        List<Integer> operatorAids = logsMapper.getAllLogsOperatorAid();
        List<String> operationTypes = logsMapper.getAllOperationType();

        List<String> names = violationsMapper.getNamesByVioIds(vioIds);
        List<String> studentIds = violationsMapper.getStudentIdsByVioIds(vioIds);
        List<String> adminNames = adminsMapper.getNamesByIds(operatorAids);

        List<LogDTO> logDTOs = new ArrayList<>();
        for (int i = 0; i < logIds.size(); i++) {
            String logId = logIds.get(i);
            String operationType = operationTypes.get(i);
            String name = names.get(i);
            String studentId = studentIds.get(i);
            String operatorName = adminNames.get(i);
            Integer operatorId = operatorAids.get(i);
            LocalDateTime operationTime = operationTimes.get(i);

            LogDTO logDTO = new LogDTO();
            logDTO.setLogId(logId);
            logDTO.setOperationType(operationType);
            logDTO.setName(name);
            logDTO.setStudentId(studentId);
            logDTO.setOperatorName(operatorName);
            logDTO.setOperatorId(operatorId);
            logDTO.setOperationTime(operationTime);

            logDTOs.add(logDTO);
        }
        return logDTOs;
    }

    @Override
    public List<LogDTO> select(SelectRequest request) {
        QueryWrapper<Logs> queryWrapper = new QueryWrapper<>();

        if (request.getStuId() != null) {
            queryWrapper.eq("v.student_id", request.getStuId());
        }
        if (request.getStuName() != null) {
            queryWrapper.like("v.name", request.getStuName());
        }
        if (request.getOperatorId() != null) {
            queryWrapper.eq("l.operator_aid", request.getOperatorId());
        }
        if (request.getOperatorName() != null) {
            queryWrapper.like("a.login_name", request.getOperatorName());
        }
        if (request.getOperationType() != null) {
            queryWrapper.eq("l.operation_type", request.getOperationType());
        }
        if (request.getStartTime() != null && request.getEndTime() != null) {
            queryWrapper.between("l.operation_time", request.getStartTime(), request.getEndTime());
        } else if (request.getStartTime() != null) {
            queryWrapper.ge("l.operation_time", request.getStartTime());
        } else if (request.getEndTime() != null) {
            queryWrapper.le("l.operation_time", request.getEndTime());
        }

        return logsMapper.selectLogs(queryWrapper);
    }

}
